package cn.iocoder.yudao.module.system.service.suggestion;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.system.controller.admin.suggestion.vo.SuggestionPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.suggestion.vo.SuggestionSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.partnerapply.PartnerApplyDO;
import cn.iocoder.yudao.module.system.dal.dataobject.suggestion.SuggestionDO;
import cn.iocoder.yudao.module.system.dal.mysql.suggestion.SuggestionMapper;
import cn.iocoder.yudao.module.system.enums.common.DealStatusEnum;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SUGGESTION_NOT_EXISTS;

/**
 * 意见建议 Service 实现类
 *
 * @author 万民匠心
 */
@Service
@Validated
public class SuggestionServiceImpl implements SuggestionService {

    @Resource
    private SuggestionMapper suggestionMapper;
    @Resource
    private MemberUserApi memberUserApi;

    @Override
    public Long createSuggestion(SuggestionSaveReqVO createReqVO) {
        // 插入
        SuggestionDO suggestion = BeanUtils.toBean(createReqVO, SuggestionDO.class);
        suggestionMapper.insert(suggestion);
        // 返回
        return suggestion.getId();
    }

    @Override
    public Long createSuggestion(Long userId, SuggestionSaveReqVO createReqVO) {
        // 插入
        SuggestionDO suggestion = BeanUtils.toBean(createReqVO, SuggestionDO.class);
        suggestion.setUserId(userId);
        suggestion.setStatus(DealStatusEnum.INIT.getStatus());
        suggestionMapper.insert(suggestion);
        // 返回
        return suggestion.getId();
    }

    @Override
    public void updateSuggestion(SuggestionSaveReqVO updateReqVO) {
        // 校验存在
        validateSuggestionExists(updateReqVO.getId());
        // 更新
        SuggestionDO updateObj = BeanUtils.toBean(updateReqVO, SuggestionDO.class);
        suggestionMapper.updateById(updateObj);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        // 校验用户存在
        validateSuggestionExists(id);
        // 更新状态
        SuggestionDO updateObj = new SuggestionDO();
        updateObj.setId(id);
        updateObj.setStatus(status);
        suggestionMapper.updateById(updateObj);
    }

    @Override
    public void deleteSuggestion(Long id) {
        // 校验存在
        validateSuggestionExists(id);
        // 删除
        suggestionMapper.deleteById(id);
    }

    private void validateSuggestionExists(Long id) {
        if (suggestionMapper.selectById(id) == null) {
            throw exception(SUGGESTION_NOT_EXISTS);
        }
    }

    @Override
    public SuggestionDO getSuggestion(Long id) {
        return suggestionMapper.selectById(id);
    }

    @Override
    public PageResult<SuggestionDO> getSuggestionPage(SuggestionPageReqVO pageReqVO) {
        // 获得用户编号
        if (StrUtil.isNotEmpty(pageReqVO.getNickname())) {
            List<MemberUserRespDTO> users = memberUserApi.getUserListByNickname(pageReqVO.getNickname());
            if (CollUtil.isEmpty(users)) {
                return PageResult.empty();
            }
            pageReqVO.setUserIds(convertSet(users, MemberUserRespDTO::getId));
        }
        return suggestionMapper.selectPage(pageReqVO);
    }

}